<?php
namespace app\walletyhbbadmin\controller;
use Think\Db;
use Think\request;
require_once (APP_PATH .'api/FinanceApi.php');
class Shipping extends AdminBase {
    public function _initialize(){
        parent::_initialize();
    }

    //首页
    public function index(){
        $list = Db::name('shipping_area')->select();
//        $region = Db::name('region')->where('level',1)->select();
        foreach($list as $k=>$v){
            if($v['province']!==''||$v['province']!==null){   //如果有范围ID
                $arr = explode(',',$v['province_id']);
                $where['id'] = array('in',$arr);
                $where['level'] = 1;
                $region = Db::name('region')->where($where)->select();
                foreach($region as $key=>$value){
                    $reg[] = $value['name'];
                }
                $list[$k]['content'] = implode(',',$reg);
                unset($arr);
                unset($reg);
            }else{
                $list[$k]['content'] = '';
            }
        }
        $this->assign('list',$list);
        return $this->fetch();
    }



    //新增配送页面
    public function addShipping(){
        if($_REQUEST['shipping_id']){
            $where['shipping_area_id'] = array('neq',$_REQUEST['shipping_id']);
            $info = Db::name('shipping_area')->where('shipping_area_id',$_REQUEST['shipping_id'])->find();
            $this_arr = explode(',',$info['province_id']);
            $this->assign('info',$info);
        }
        $region = Db::name('region')->where('level',1)->select();
        $list = Db::name('shipping_area')->where($where)->select();
        foreach($region as $k=>$v){
            foreach($list as $key=>$value){
                $arr = explode(',',$value['province_id']);
                if(in_array($v['id'],$arr)){
                    unset($region[$k]);
                }
            }
        }
        if($_REQUEST['shipping_id']){
            foreach($region as $k=>$v){
                if(in_array($v['id'],$this_arr)){
                    $region[$k]['bl'] = 1;
                }else{
                    $region[$k]['bl'] = 0;
                }
            }
        }
        $this->assign('region',$region);
        return $this->fetch('add');
    }


    //新增或修改提交方法
    public function addShippingSub(){
        if($_POST['region']){    //判断是否有配送范围
            $arr['province_id'] = implode(',',$_POST['region']);    //将上传的数组转换为字符串存入数据库
            $arr['shipping_area_name'] = trim(input('shipping_area_name'));
            if($arr['shipping_area_name']==''){
                $this->error('请输入配送名称');
            }
            if($_POST['shipping_area_id']==''){   //判断是否是新增
//                dump($_POST['shipping_area_id']);  die();
                //如果是新增  还需要添加额外数据
                $shipping_money['shipping_id'] = Db::name('shipping_area')->insertGetId($arr);   //新增并获取ID
                $this->success('添加成功',U('Shipping/index'));
            }else{
                $res = Db::name('shipping_area')->where('shipping_Area_id',$_POST['shipping_area_id'])->update($arr);
                if($res!==false){
                    $this->success('修改成功',U('Shipping/index'));
                }else{
                    $this->error('操作失败');
                }
            }
        }else{
            $this->error('请选择配送范围');
        }
    }










}